﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MvvmEx.Command;

namespace MvvmEx.Controls.Calendar
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:MvvmEx.Controls.Calendar"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:MvvmEx.Controls.Calendar;assembly=MvvmEx.Controls.Calendar"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误: 
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:MarkerCalendarItem/>
    ///
    /// </summary>
    internal class MarkerCalendarItem : Control
    {
        #region 字段

        private MarkerCalendar _calendar;

        #endregion

        #region 依赖属性

        #region 是否显示删除按钮

        public bool IsShowDeleteBtn
        {
            get { return (bool)GetValue(IsShowDeleteBtnProperty); }
            set { SetValue(IsShowDeleteBtnProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsShowDeleteBtn.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsShowDeleteBtnProperty =
            DependencyProperty.Register("IsShowDeleteBtn", typeof(bool), typeof(MarkerCalendarItem), new PropertyMetadata(false));

        #endregion

        #region 是否具有值
        public bool HasValue
        {
            get
            {
                return (bool)GetValue(HasValueProperty);
            }
            set
            {
                SetValue(HasValueProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for HasValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HasValueProperty =
            DependencyProperty.Register("HasValue", typeof(bool), typeof(MarkerCalendarItem), new PropertyMetadata(false));
        #endregion

        #region 行
        public int Row
        {
            get
            {
                return (int)GetValue(RowProperty);
            }
            set
            {
                SetValue(RowProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for Row.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RowProperty =
            DependencyProperty.Register("Row", typeof(int), typeof(MarkerCalendarItem), new PropertyMetadata(0,
                (s, e) =>
                {
                    var obj = s as MarkerCalendarItem;

                    if (obj != null)
                    {
                        Grid.SetRow(obj, (int)e.NewValue);
                    }
                }));
        #endregion

        #region 列
        public int Column
        {
            get
            {
                return (int)GetValue(ColumnProperty);
            }
            set
            {
                SetValue(ColumnProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for Column.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ColumnProperty =
            DependencyProperty.Register("Column", typeof(int), typeof(MarkerCalendarItem), new PropertyMetadata(0,
                (s, e) =>
                {
                    var obj = s as MarkerCalendarItem;

                    if (obj != null)
                    {
                        Grid.SetColumn(obj, (int)e.NewValue);
                    }
                }));
        #endregion

        #region 天
        public int Day
        {
            get
            {
                return (int)GetValue(DayProperty);
            }
            set
            {
                SetValue(DayProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for Day.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DayProperty =
            DependencyProperty.Register("Day", typeof(int), typeof(MarkerCalendarItem), new PropertyMetadata(0));



        #endregion

        #region 日期
        public DateTime DateTime
        {
            get
            {
                return (DateTime)GetValue(DateTimeProperty);
            }
            set
            {
                SetValue(DateTimeProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for DateTime.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DateTimeProperty =
            DependencyProperty.Register("DateTime", typeof(DateTime), typeof(MarkerCalendarItem), new PropertyMetadata(DateTime.Now,
                (s, e) =>
                {
                    var obj = s as MarkerCalendarItem;

                    if (obj == null) return;

                    obj.Day = obj.DateTime.Day;
                }));
        #endregion

        #region 内容
        public string Content
        {
            get
            {
                return (string)GetValue(ContentProperty);
            }
            set
            {
                SetValue(ContentProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(string), typeof(MarkerCalendarItem), new PropertyMetadata(null));



        #endregion

        #region ID



        public string Id
        {
            get { return (string)GetValue(IdProperty); }
            set { SetValue(IdProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Id.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IdProperty =
            DependencyProperty.Register("Id", typeof(string), typeof(MarkerCalendarItem), new PropertyMetadata(string.Empty));



        #endregion

        #endregion

        #region 构造
        static MarkerCalendarItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MarkerCalendarItem), new FrameworkPropertyMetadata(typeof(MarkerCalendarItem)));

        }

        public MarkerCalendarItem(MarkerCalendar calendar)
        {
            _calendar = calendar;
            IsShowDeleteBtn = _calendar.IsShowOkBtn;
        }
        #endregion

        #region 重载

        #region 获取部件

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            var border = GetTemplateChild("PART_Border") as Border;

            if (border != null)
            {
                border.MouseLeftButtonDown += Border_MouseLeftButtonDown;
            }

            var delBtn = GetTemplateChild("DelBtn") as Button;

            if (delBtn != null)
            {
                delBtn.Click += DelBtn_Click;
            }
        }

        private void DelBtn_Click(object sender, RoutedEventArgs e)
        {
            _calendar.OnDeleteEvent(new MarkerCalendarDataItem() { Id = this.Id, Content = this.Content, DateTime = this.DateTime });
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                var dataItem = new MarkerCalendarDataItem() { Id = Id, DateTime = DateTime, Content = Content };
                _calendar.OnDoubleClickEvent(dataItem);
            }
        }

        #endregion

        #endregion



    }
}
